import 'package:enjoy_plus/models/home_noticelist_model.dart';
import 'package:enjoy_plus/utils/http.dart';
import 'package:enjoy_plus/utils/logger.dart';
import 'package:enjoy_plus/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key, required this.id});
  final String id;

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  NotifyItem notifyDetail = NotifyItem(
      id: '', content: '', title: '', createdAt: '', creatorName: '');

  void getNotifyDetail() async {
    try {
      final response = await http.get<NotifyItem>(
        '/announcement/${widget.id}',
        fromJson: (data) {
          return NotifyItem.fromJson(data);
        },
      );

      if (response.code == 10000) {
        setState(() {
          notifyDetail = response.data;
        });
        logger.d('notifyDetail: ${notifyDetail.toString()}');
        ToastUtil.showSuccess('获取数据成功');
      } else {
        ToastUtil.showError(response.message);
      }
    } catch (e) {
      ToastUtil.showError('获取数据失败');
      logger.e(e);
    }
  }

  @override
  void initState() {
    super.initState();
    getNotifyDetail();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
          title: Text('公告详情'),
          leading: IconButton(
              onPressed: () => Navigator.pop(context),
              icon: const Icon(Icons.arrow_back))),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(notifyDetail.title,
                  style: const TextStyle(
                      fontSize: 18, fontWeight: FontWeight.bold)),
              const SizedBox(height: 10),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(notifyDetail.creatorName,
                      style: const TextStyle(fontSize: 12, color: Colors.grey)),
                  Text(notifyDetail.createdAt,
                      style: const TextStyle(fontSize: 12, color: Colors.grey))
                ],
              ),
              const SizedBox(height: 10),
              HtmlWidget(notifyDetail.content)
            ],
          ),
        ),
      ),
    );
  }
}
